const { studentsModel } = require('../models/studentsModel');
const { moveFiles, deleteFiles } = require('../utils/handleFiles')

async function addStudents(req, res, next) {
    const data = req.body;
    const result = await studentsModel.create(data);
    if (result.image) {
        // 将图片从临时文件夹移动到正式文件夹
        moveFiles({
            fromPath: './public/temp',
            toPath: './public/images',
            filename: result.image
        });
        deleteFiles('./public/temp');
    }
    res.send({
        message: '新增成功',
        code: 1
    });
}

async function getStudents(req, res, next) {
    const data = req.query;
    // 方法一：精确查询，【查询部分】和【获取所有】分开判断 ----------------------------------------------------------
    // let result = [];
    // if (data.value) {
    //     // 精确查询
    //     result = await studentsModel.find({ [data.type]: data.value }).populate('classesId');
    // } else {
    //     // 获取所有学生
    //     // 获取学生数据的同时，关联查询学生对应的班级数据
    //     result = await studentsModel.find().populate('classesId');
    // }
    // 方法一：----------------------------------------------------------

    // 方法二：模糊查询，【查询部分】和【获取所有】通过正则写在一起 -------------------------------------------
    // 获取学生数据的总条数
    const total = await studentsModel.countDocuments({
        [data.type]: { $regex: data.value, $options: '$i' }
    });
    // 计算总页数
    const pages = Math.ceil(total / data.pageSize);
    // 分页获取学生数据
    const result = await studentsModel
        .find({ [data.type]: { $regex: data.value, $options: '$i' } })
        .populate('classesId')
        // .populate({
        //     path: 'classesId',
        //     populate: {
        //         path: 'teachersId'
        //     }
        // })
        .limit(data.pageSize - 0) // 请求的数据条数
        .skip(data.pageSize * (data.currentPage - 1))  // 跳过的数据条数
    // 方法二：----------------------------------------------------------


    res.send({
        message: '学生数据获取成功',
        code: 1,
        data: { total, pages, result }
    })

    // res.jsonp({
    //     message: '学生数据获取成功',
    //     code: 1,
    //     data: { total, pages, result }
    // })
}

async function deleteStudents(req, res) {
    const data = req.body;
    const result = await studentsModel.deleteOne(data);
    if (result.deletedCount > 0) {
        res.send({
            message: '删除成功',
            code: 1
        })
    } else {
        res.send({
            message: '删除失败',
            code: 0
        })
    }
}

async function getStudentsById(req, res) {
    // get  req.query
    // const data = req.query;
    const data = req.params;
    console.log('data', data);
    const result = await studentsModel.find(data);
    if (result.length > 0) {
        res.send({
            message: '成功获取学生数据',
            code: 1,
            data: result[0]
        })
    } else {
        res.send({
            message: '学生数据获取失败',
            code: 0
        })
    }
}

async function updateStudents(req, res) {
    const data = req.body;
    const result = await studentsModel.updateOne({ _id: data._id }, data);
    if (result.nModified > 0) {
        res.send({
            message: '修改成功',
            code: 1
        })
    } else {
        res.send({
            message: '修改失败',
            code: 0
        })
    }
}

module.exports = {
    addStudents, getStudents, deleteStudents, getStudentsById, updateStudents
}